﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using HotelManagement.Class;
using System.Configuration;
using System.Collections;
namespace HotelManagement
{
    public partial class CreateStaff : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                getReportToItems();
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            Server.Transfer("ManageHousekeepingAndStaff.aspx");
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            clearTextBox();
        }

        protected void btnCreateStaff_Click1(object sender, EventArgs e)
        {
            createStaffAndBankAccount();
        }
        private void getReportToItems()
        {
            ddlReportTo.Items.Clear();
            string selectSQL = "SELECT FirstName,LastName FROM Staff";

            SqlDataReader reader;
            ListItem newItem = new ListItem();

            try
            {
                SqlConnection con = StaffDBManager.ConnectToDatabase();

                SqlCommand cmd = new SqlCommand(selectSQL, con);

                con.Open();
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    newItem = new ListItem();
                    newItem.Text = reader["FirstName"].ToString() + " " + reader["LastName"].ToString();
                    newItem.Value = reader["FirstName"].ToString() + " " + reader["LastName"].ToString();
                    ddlReportTo.Items.Add(newItem);
                }
                reader.Close();

            }
            catch (SqlException ex1)
            {
                lblStatus.Text = "Error: " + ex1 + "Please try again";
            }
        }

        private void createStaffAndBankAccount()
        {
            try
            {
                //Get Age
                DateTime today = DateTime.Today;
                int dOBYear = (int)Convert.ToInt32(tbxYear.Text);
                int age = today.Year - dOBYear;

                //Get Date Of Birth
                DateTime dOB = (DateTime)Convert.ToDateTime(tbxDay.Text + ddlMonth.SelectedValue + tbxYear.Text);

                int mobileNum = (int)Convert.ToInt32(tbxMobileNumber.Text);
                int homeNum = (int)Convert.ToInt32(tbxHomeNumber.Text);
             
                // Create Staff
                Staff s = new Staff(tbxStaffID.Text, tbxFirstName.Text, tbxLastName.Text, age, dOB, rblGender.SelectedValue, tbxEmail.Text, tbxBlockNum.Text, tbxStreetName.Text, tbxUnitNum.Text, tbxCountry.Text, tbxZIPCode.Text, ddlDutyType.SelectedValue, tbxTitle.Text, mobileNum, homeNum, ddlReportTo.SelectedValue, tbxNotes.Text);

                // Create Bank Account Staff
                //Display Create success or fail
                BankAccountStaff ba = new BankAccountStaff(tbxAccountNumber.Text, tbxAccountName.Text, ddlAccountType.SelectedValue, tbxTransitCode.Text, tbxBankName.Text, tbxBankBranch.Text, tbxStaffID.Text);
                if (StaffDBManager.insertStaff(s) == 1 && BankAccountStaffDBManager.insertBankAccountStaff(ba) == 1)
                {
                    lblStatus.Text = String.Empty;
                    lblStatus.Text = "Create New Staff Successful";
                }
                else
                {
                    lblStatus.Text = "Create failed. Please try again";
                }

            }
            catch (Exception e)
            {
                lblStatus.Text = "The Staff you are trying to create is already in the Database. Check the Staff ID.";
            }
        }

        private void clearTextBox()
        {
            // Clear General Information
            tbxStaffID.Text = String.Empty;
            tbxTitle.Text = String.Empty;
            tbxFirstName.Text = String.Empty;
            tbxLastName.Text = String.Empty;
            tbxDay.Text = String.Empty;
            tbxYear.Text = String.Empty;

            //Clear Contact Information
            tbxMobileNumber.Text = String.Empty;
            tbxHomeNumber.Text = String.Empty;
            tbxEmail.Text = String.Empty;
            tbxBlockNum.Text = String.Empty;
            tbxStreetName.Text = String.Empty;
            tbxUnitNum.Text = String.Empty;
            tbxCountry.Text = String.Empty;
            tbxZIPCode.Text = String.Empty;

            //Clear Bank Account Information
            tbxAccountName.Text = String.Empty;
            tbxAccountNumber.Text = String.Empty;
            tbxTransitCode.Text = String.Empty;
            tbxBankName.Text = String.Empty;
            tbxBankBranch.Text = String.Empty;

            //Clear Additional Information
            tbxNotes.Text = String.Empty;

            //Clear label
            lblStatus.Text = String.Empty;

        }




    }
}